{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    },
    "accelerator": "GPU",
    "gpuClass": "standard"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Setup and Installation"
      ],
      "metadata": {
        "id": "We6Tr7MockoZ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!pip3 install openmim\n",
        "!mim install mmengine\n",
        "!mim install 'mmcv>=2.0.0rc1'\n",
        "!mim install 'mmdet>=3.0.0rc0'\n",
        "!git clone https://github.com/open-mmlab/mmocr.git\n",
        "%cd mmocr\n",
        "!git checkout 1.x\n",
        "!pip3 install -e ."
      ],
      "metadata": {
        "id": "kvELnLgCjM8Y"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [],
      "metadata": {
        "id": "CnifrfAScis0"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Check Pytorch installation\n",
        "import torch, torchvision\n",
        "print(torch.__version__, torch.cuda.is_available())\n",
        "\n",
        "# Check MMDetection installation\n",
        "import mmdet\n",
        "print(mmdet.__version__)\n",
        "\n",
        "# Check mmcv installation\n",
        "import mmcv\n",
        "from mmcv.ops import get_compiling_cuda_version, get_compiler_version\n",
        "print(mmcv.__version__)\n",
        "print(get_compiling_cuda_version())\n",
        "print(get_compiler_version())\n",
        "\n",
        "# Check mmocr installation\n",
        "import mmocr\n",
        "print(mmocr.__version__)"
      ],
      "metadata": {
        "id": "MOSlDJoYlKM2"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/content/drive')"
      ],
      "metadata": {
        "id": "D9Is_iz6PfMu"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Inference"
      ],
      "metadata": {
        "id": "G93FVD_icrzm"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from mmocr.ocr import MMOCR\n",
        "\n",
        "img_directory = '/content/drive/MyDrive/Analogue Gauge Reading/task detection - data/detection/train/images/'\n",
        "img_path =  img_directory + '20220613_193719573_iOS_jpg.rf.ff724329334c6028e226cbecfc15b6b2.jpg'\n",
        "o_path = '/content/drive/MyDrive/Analogue Gauge Reading/'\n",
        "\n",
        "# Load models into memory\n",
        "ocr = MMOCR(det='DB_r18', recog='ABINet')\n",
        "\n",
        "# Inference\n",
        "results = ocr.readtext(img_path, img_out_dir=o_path)"
      ],
      "metadata": {
        "id": "pCUc_HtTQSD6"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# import required module\n",
        "import os\n",
        "# assign directory\n",
        "img_directory = '/content/drive/MyDrive/Analogue Gauge Reading/task detection - data/detection/train/images/'\n",
        " \n",
        "# iterate over files in\n",
        "# that directory\n",
        "filenames = []\n",
        "for filename in os.listdir(img_directory):\n",
        "    filenames.append(img_directory + filename)"
      ],
      "metadata": {
        "id": "1oZA3aOomsUv"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from tqdm import tqdm\n",
        "\n",
        "det_model = 'DBPP_r50'\n",
        "recog_model = 'ABINet'\n",
        "\n",
        "o_path = '/content/drive/MyDrive/Analogue Gauge Reading/mmocr/' + det_model + \" - \" + recog_model\n",
        "\n",
        "# Load models into memory\n",
        "ocr = MMOCR(det=det_model, recog=recog_model)\n",
        "\n",
        "for filename in tqdm(filenames[0:50]):\n",
        "   results = ocr.readtext(filename, img_out_dir=o_path)"
      ],
      "metadata": {
        "id": "YH00HP6d8Nys"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "len(filenames)"
      ],
      "metadata": {
        "id": "xMCbb2HJ8uCR"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "det_model = 'PANet_IC15'\n",
        "recog_model = 'ABINet'\n",
        "\n",
        "o_path = '/content/drive/MyDrive/Analogue Gauge Reading/mmocr/' + det_model + \" - \" + recog_model"
      ],
      "metadata": {
        "id": "uw3gOlK8D56o"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}
